package org.argus.management.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.argus.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.argus.management.domain.User;
import org.argus.management.system.model.*;
import org.argus.management.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author 刘文/Cliff Liu
 * @Date 2024/6/10
 */
@Api("用户管理")
@RestController
@RequestMapping("/system/v1/user")
public class UserController {


    @Autowired
    private UserService userService;

    @ApiOperation("用户分页搜索")
    @GetMapping("/page")
    public R<Page<User>> page(UserSearchReq req, Page page) {
        return R.ok(userService.page(req, page));
    }

    @ApiOperation("保存用户")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody SaveUserReq req) {
        userService.save(req);
        return R.ok(Boolean.TRUE);
    }

    @ApiOperation("逻辑删除用户")
    @DeleteMapping
    public R<Void> delete(@RequestBody @Valid DeleteUsersReq req) {
        userService.delete(req.getIds());
        return R.ok();
    }

    @ApiOperation("用户角色ID列表")
    @GetMapping("/user-role-ids/{id}")
    public R<List<Long>> userRoleIds(@PathVariable("id") Long id) {
        return R.ok(userService.userRoleIds(id));
    }

    @ApiOperation("修改用户密码")
    @PutMapping("/update-password")
    public R<Void> updatePassword(@RequestBody @Valid UpdateUserPasswordReq updateReq) {
        userService.updatePassword(updateReq);
        return R.ok();
    }

    @ApiOperation("保存用户")
    @PutMapping("/user-role-rel")
    public R<Void> saveUserRoleRel(@RequestBody @Valid UserRoleBindReq req) {
        userService.saveUserRoleRel(req);
        return R.ok();
    }
}
